<template>
  <div class="appointment-container">
    <el-card class="table-card" :body-style="{ padding: '0' }">
      <template #header>
        <div class="header">
          <h2>预约退款列表</h2>
          <div class="header-right">
            <el-tag type="info" effect="dark" class="header-tag">待处理: {{ pendingCount }}</el-tag>
          </div>
        </div>
      </template>

      <div class="table-container">
        <el-table 
          :data="appointments" 
          style="width: 100%" 
          v-loading="loading"
          element-loading-text="加载中..."
          element-loading-background="rgba(255, 255, 255, 0.8)"
          :header-cell-style="{background:'#f8faff', color: '#1a237e'}"
          :row-class-name="tableRowClassName"
        >
          <el-table-column prop="roomName" label="会议室" min-width="120">
            <template #default="scope">
              <div class="cell-with-icon">
                <i class="el-icon-office-building cell-icon"></i>
                <span>{{ scope.row.roomName }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="bookingDate" label="日期" min-width="120">
            <template #default="scope">
              <div class="cell-with-icon">
                <i class="el-icon-date cell-icon"></i>
                <span>{{ scope.row.bookingDate }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="startHour" label="开始时间" min-width="120">
            <template #default="scope">
              <div class="cell-with-icon">
                <i class="el-icon-time cell-icon"></i>
                <span>{{ formatTime(scope.row.startHour) }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="endHour" label="结束时间" min-width="120">
            <template #default="scope">
              <div class="cell-with-icon">
                <i class="el-icon-time cell-icon"></i>
                <span>{{ formatTime(scope.row.endHour) }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" min-width="100">
            <template #default="scope">
              <el-tag 
                :type="getStatusType(scope.row.status)" 
                effect="light"
                class="status-tag"
              >
                {{ getStatusText(scope.row.status) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="180" fixed="right">
            <template #default="scope">
              <div class="action-buttons">
                <el-button
                  v-if="scope.row.status === 'pending'"
                  type="success"
                  size="small"
                  @click="handleApprove(scope.row)"
                  class="action-button approve-button"
                >
                  <i class="el-icon-check"></i> 批准
                </el-button>
                <el-button
                  v-if="scope.row.status === 'pending'"
                  type="danger"
                  size="small"
                  @click="handleReject(scope.row)"
                  class="action-button reject-button"
                >
                  <i class="el-icon-close"></i> 拒绝
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { useAppointmentStore } from '../stores/AppointmentStore'
const appointmentStore = useAppointmentStore()

// 表格数据
const appointments = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 计算待处理的预约数量
const pendingCount = computed(() => {
  return appointments.value.filter(item => item.status === 'pending').length
})

// 表格行样式
const tableRowClassName = ({ row }) => {
  if (row.status === 'pending') return 'pending-row'
  return ''
}

// 筛选表单
const filterForm = reactive({
  roomId: '',
  date: ''
})

// 对话框相关
const dialogVisible = ref(false)
const dialogType = ref('add')
const appointmentFormRef = ref(null)
const appointmentForm = reactive({
  roomId: '',
  date: '',
  timeRange: []
})

// 表单验证规则
const appointmentRules = {
  roomId: [
    { required: true, message: '请选择会议室', trigger: 'change' }
  ],
  date: [
    { required: true, message: '请选择日期', trigger: 'change' }
  ],
  timeRange: [
    { required: true, message: '请选择时间范围', trigger: 'change' }
  ]
}

// 获取预约列表
const fetchAppointments = async () => {
  loading.value = true
  try {
    const response = await appointmentStore.fetchPendingAppointments()
    if (response.code === 200) {
      appointments.value = response.data
      total.value = response.data.length
    } else {
      ElMessage.error(response.message || '获取预约列表失败')
    }
  } catch (error) {
    console.error('获取预约列表失败:', error)
    ElMessage.error('获取预约列表失败')
  } finally {
    loading.value = false
  }
}

// 组件挂载时获取数据
onMounted(() => {
  fetchAppointments()
})

// 修改格式化方法
const formatTime = (time) => {
  if (!time) return '未设置'
  // 处理时间字符串
  return time.slice(0, 5) // 取出HH:mm部分
}

// 获取状态类型
const getStatusType = (status) => {
  switch (status) {
    case 'approved':
      return 'success'
    case 'pending':
      return 'warning'
    case 'rejected':
      return 'danger'
    case 'cancelled':
      return 'info'
    default:
      return 'info'
  }
}

// 获取状态文本
const getStatusText = (status) => {
  switch (status) {
    case 'approved':
      return '已批准'
    case 'pending':
      return '待审核'
    case 'rejected':
      return '已拒绝'
    case 'cancelled':
      return '已取消'
    default:
      return status
  }
}


// 重置筛选
const resetFilter = () => {
  filterForm.roomId = ''
  filterForm.date = ''
  handleSearch()
}
var SubmitOrReject = {
    "id" : 0,
    "staffId" : 0
  }
// 处理批准
const handleApprove = async (row) => {
  SubmitOrReject.id = row.id
  SubmitOrReject.staffId = 3
  console.log(SubmitOrReject)
  try {
    const response = await appointmentStore.approveAppointment(SubmitOrReject.id, SubmitOrReject.staffId)
    if (response.code === 200) {
      ElMessage.success('已批准预约')
      fetchAppointments()
    } else {
      ElMessage.error(response.message || '操作失败')
    }
  } catch (error) {
    console.error('操作失败:', error)
    ElMessage.error('操作失败')
  }
}

// 处理拒绝
const handleReject = async (row) => {
  SubmitOrReject.id = row.id
  SubmitOrReject.staffId = 3
  try {
    const response = await appointmentStore.rejectAppointment(SubmitOrReject.id, SubmitOrReject.staffId)
    if (response.code === 200) {
      ElMessage.success('已拒绝预约')
      fetchAppointments()
    } else {
      ElMessage.error(response.message || '操作失败')
    }
  } catch (error) {
    console.error('操作失败:', error)
    ElMessage.error('操作失败')
  }
}
</script>

<style scoped>
.appointment-container {
  padding: 0px 24px 32px 24px;
  min-height: 100vh;
  margin-left: 12px;
  background-color: #f5f7fa;
  background-image: linear-gradient(135deg, #f5f7fa 0%, #eef2f7 100%);
}

/* 卡片样式增强 */
.table-card {
  background: white;
  border-radius: 18px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: all 0.3s ease;
  border: none;
  margin: 0 0 16px 0;
  transform: translateY(0);
}

.table-card:hover {
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.12);
  transform: translateY(-2px);
}

.card-header {
  position: sticky;
  top: 0;
  padding: 20px 24px;
  border-bottom: 1px solid #eef2f7;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #3949ab 0%, #5c6bc0 100%);
  z-index: 20;
  transition: all 0.3s ease;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.13);
  border-radius: 0 0 18px 18px;
}

.filter-container {
  position: sticky;
  top: 72px;
  z-index: 15;
  padding: 20px 24px 12px 24px;
  border-bottom: 1px solid #eef2f7;
  background: rgba(252, 253, 255, 0.98);
  box-shadow: 0 2px 8px rgba(57, 73, 171, 0.06);
  border-radius: 0 0 14px 14px;
  margin-bottom: 12px;
}

.appointment-container {
  padding: 0px 24px 32px 24px;
  min-height: 100vh;
  margin-left: 12px;
  background-color: #f5f7fa;
  background-image: linear-gradient(135deg, #f5f7fa 0%, #eef2f7 100%);

}

.table-container {
  padding: 0;
  overflow-x: auto;
}

/* 表格容器 */
.table-container {
  padding: 0;
  overflow-x: auto;
}

/* 表格样式增强 */
:deep(.el-table) {
  border-radius: 0;
  overflow: hidden;
  --el-table-border-color: #eef2f7;
  --el-table-header-bg-color: #f8faff;
}

:deep(.el-table::before) {
  display: none;
}

:deep(.el-table th) {
  background-color: #f0f4ff !important;
  color: #0d1757;
  font-weight: 700;
  padding: 14px 16px;
  border-bottom: 2px solid #eef2f7;
  font-size: 15px;
}

:deep(.el-table td) {
  color: #2c3e50;
  padding: 14px 16px;
  border-bottom: 1px solid #eef2f7;
  font-size: 14px;
}

:deep(.el-table .pending-row) {
  background-color: rgba(255, 248, 227, 0.3);
}

:deep(.el-table .pending-row:hover > td) {
  background-color: rgba(255, 248, 227, 0.5) !important;
}

:deep(.el-table .el-table__row:hover > td) {
  background-color: #f8faff;
}

/* 单元格内容样式 */
.cell-with-icon {
  display: flex;
  align-items: center;
}

.cell-icon {
  color: #1a237e;
  margin-right: 8px;
  font-size: 16px;
  opacity: 0.9;
}

/* 状态标签样式 */
.status-tag {
  border-radius: 20px;
  padding: 4px 12px;
  font-weight: 600;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 80px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  letter-spacing: 0.5px;
}

/* 操作按钮样式 */
.action-buttons {
  display: flex;
  gap: 8px;
}

.action-button {
  padding: 6px 12px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.approve-button {
  background: linear-gradient(90deg, #2e7d32 0%, #43a047 100%);
  border: none;
  color: white;
}

.approve-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(46, 125, 50, 0.3);
}

.reject-button {
  background: linear-gradient(90deg, #c62828 0%, #d32f2f 100%);
  border: none;
  color: white;
}

.reject-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(198, 40, 40, 0.3);
}

/* 分页容器样式 */
.pagination-container {
  padding: 16px 20px;
  display: flex;
  justify-content: flex-end;
  border-top: 1px solid #eef2f7;
  background-color: #ffffff;
}

:deep(.el-pagination) {
  --el-pagination-button-bg-color: white;
  --el-pagination-hover-color: #3949ab;
  --el-pagination-bg-color: transparent;
}

:deep(.el-pagination .el-pagination__total) {
  color: #546e7a;
}

:deep(.el-pagination .el-pagination__jump) {
  color: #546e7a;
}

:deep(.el-pagination .el-input__inner) {
  border-radius: 6px;
}

:deep(.el-pagination .btn-prev),
:deep(.el-pagination .btn-next) {
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-pagination .btn-prev:hover),
:deep(.el-pagination .btn-next:hover) {
  border-color: #3949ab;
  color: #3949ab;
}

:deep(.el-pagination .el-pager li) {
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-pagination .el-pager li.active) {
  background: #3949ab;
  color: white;
  font-weight: 600;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .appointment-container {
    padding: 12px 10px;
  }

  .page-header {
    padding: 12px 0;
  }

  .page-title {
    font-size: 22px;
  }

  .card-header {
    padding: 14px;
    flex-direction: column;
    align-items: flex-start;
  }

  .header-right {
    margin-top: 8px;
  }

  .pagination-container {
    padding: 14px;
  }

  .action-buttons {
    flex-direction: column;
  }

  :deep(.el-table th),
  :deep(.el-table td) {
    padding: 10px 12px;
  }
}
</style>